from sockets.frontsocket import sio
from sockets.agvsocket import AgvSocket

from settings.settings import settings

agv_sio_instance = AgvSocket()


@sio.on('forward')
async def forward(sid, data):
    print('forward')
    print(data)
    agv_sio_instance.send(agv_sio_instance.get(settings.AGV_PORT_CONTROL), 1, 2010, {"vx": 0.050000, "vy": 0.000000, "w": 0.000000})
    res = agv_sio_instance.receive(agv_sio_instance.get(settings.AGV_PORT_CONTROL))
    await sio.emit('completed', res, room="agv")


@sio.on('backward')
async def backward(sid, data):
    print('backward')
    agv_sio_instance.send(agv_sio_instance.get(settings.AGV_PORT_CONTROL), 1, 2010, {"vx": -0.100000, "vy": 0.000000, "w": 0.000000})
    res = agv_sio_instance.receive(agv_sio_instance.get(settings.AGV_PORT_CONTROL))
    await sio.emit('completed', res, room="agv")


@sio.on('turnleft')
async def turnleft(sid, rad):
    print('turn left')
    agv_sio_instance.send(agv_sio_instance.get(settings.AGV_PORT_CONTROL), 1, 2010, {"vx": 0.000000, "vy": 0.000000, "w": 0.261799})
    res = agv_sio_instance.receive(agv_sio_instance.get(settings.AGV_PORT_CONTROL))
    await sio.emit('completed', res, room="agv")


@sio.on('turnright')
async def turnright(sid, rad):
    print('turn right')
    agv_sio_instance.send(agv_sio_instance.get(settings.AGV_PORT_CONTROL), 1, 2010, {"vx": 0.000000, "vy": 0.000000, "w": -0.261799})
    res = agv_sio_instance.receive(agv_sio_instance.get(settings.AGV_PORT_CONTROL))
    await sio.emit('completed', res, room="agv")


@sio.on('stop')
async def stop(sid):
    print('stop')
    agv_sio_instance.send(agv_sio_instance.get(settings.AGV_PORT_CONTROL), 1, 2000)
    res = agv_sio_instance.receive(agv_sio_instance.get(settings.AGV_PORT_CONTROL))
